package com.frxuexi.repair.view

import android.content.Intent
import android.util.Log
import android.view.View
import android.widget.AdapterView
import android.widget.ArrayAdapter
import androidx.recyclerview.widget.GridLayoutManager
import com.alibaba.android.arouter.facade.annotation.Route
import com.blankj.utilcode.util.SPUtils
import com.blankj.utilcode.util.ToastUtils
import com.frxuexi.lib_base.entity.RepairBeanItem
import com.frxuexi.lib_base.mvvm.BaseActivity
import com.frxuexi.lib_base.net.Const
import com.frxuexi.repair.RepairViewModel
import com.frxuexi.repair.dapter.RepairImgAdapter
import com.frxuexi.repair.databinding.ActivityRepairAddBinding
import com.wildma.pictureselector.PictureBean
import com.wildma.pictureselector.PictureSelector
import dagger.hilt.android.AndroidEntryPoint

@Route(path = "/repair/RepairAddActivity")
@AndroidEntryPoint
class RepairAddActivity : BaseActivity<ActivityRepairAddBinding,RepairViewModel>() {
    val repairImgAdapter by lazy { RepairImgAdapter() }
    var area="A区停车场"
    override fun initData() {
        vm.addRepairData.observe(this){
            vm.addRepairRoom(it)
            finish()
        }
        vm.addRepairRoomData.observe(this){
            ToastUtils.showLong("添加成功")
            Log.i("===",it.toString())
        }
    }

    override fun initView() {
        binding.headRepairAdd.apply {
            headerBack.setOnClickListener {
                finish()
            }
            headerTitle.setText("维修添加")
            headerAdd.setImageResource(com.frxuexi.lib_base.R.drawable.ic_action_ok)
            headerAdd.setOnClickListener {
                val phone = binding.repairaddPhone.text.toString()
                val uname = binding.repairaddUser.text.toString()
                val remark = binding.repairaddRemark.text.toString()
                val item = RepairBeanItem(
                    phone,
                    remark,
                    "",
                    null,
                    "WX${vm.getDateTimeString(System.currentTimeMillis())}",
                    "已提交",
                    area,
                    uname,
                    vm.getDateTime(System.currentTimeMillis()),
                    SPUtils.getInstance().getInt(Const.UID)
                )
                vm.addRepair(item)
            }
        }
        binding.repairaddUser.setText(SPUtils.getInstance().getString(Const.USERNAME))
        binding.repairaddImg.setOnClickListener {
            if(repairImgAdapter.data.size<4) {
                PictureSelector.create(this, PictureSelector.SELECT_REQUEST_CODE).selectPicture()
            }else{
                ToastUtils.showLong("最多只能选择4张图片")
            }
        }

        binding.repairaddRv.apply {
            adapter=repairImgAdapter
            layoutManager=GridLayoutManager(this@RepairAddActivity,4)
        }

        val areas = arrayOf("A区停车场", "B区停车场", "C区停车场")
        val areaAdapter = ArrayAdapter(this, android.R.layout.simple_spinner_item, areas)
        areaAdapter.setDropDownViewResource(android.R.layout.simple_spinner_dropdown_item)
        binding.repairaddLocarion.adapter=areaAdapter
        binding.repairaddLocarion.onItemSelectedListener=object : AdapterView.OnItemSelectedListener {
            override fun onItemSelected(
                parent: AdapterView<*>?,
                view: View?,
                position: Int,
                id: Long
            ) {
                area=areas[position]
//                areaId=position+1
            }

            override fun onNothingSelected(parent: AdapterView<*>?) {
            }

        }
    }

    override fun onActivityResult(requestCode: Int, resultCode: Int, data: Intent?) {
        super.onActivityResult(requestCode, resultCode, data)
        if(requestCode==PictureSelector.SELECT_REQUEST_CODE && resultCode== RESULT_OK){
            val bean =
                data?.extras?.getParcelable<PictureBean>(PictureSelector.PICTURE_RESULT)
            repairImgAdapter.addData(bean?.path!!)
            repairImgAdapter.notifyDataSetChanged()
        }
    }

}